package com.jykj.modbus.equipment.dao;

import com.jykj.modbus.equipment.dao.guard.EquipmentDataDaoGuard;
import com.jykj.modbus.equipment.entity.guard.EquipmentData;
import com.jykj.util.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class EquipmentDataDao extends EquipmentDataDaoGuard {

    /**查询设备方法**/
    public List<EquipmentData> selectEquipmentDataList(EquipmentData obj) {
        String hql = " from EquipmentData where 1=1";
        Map<String, Object> param = new HashMap<>();
        //设备识别码
        if (obj.getIdentificationCode() != null) {
            hql += " and identificationCode = :identificationCode";
            param.put("identificationCode",  obj.getIdentificationCode() );
        }
        //批次号
//        if (obj.getBatchNum() != null) {
//            hql += " and batchNum = :batchNum";
//            param.put("batchNum",  obj.getBatchNum() );
//        }
        return baseDao.findByHql(hql, param);
    }


    /**
     * 通过条件获取1分钟内的数据
     * 设备号 , 传感器类型 , 指令报文类型三个条件必填
     * @param obj
     * @return
     */
    public List<Map<String, Object>> selectEquipmentDataByTime(EquipmentData obj){
        String sqlLeft = " select ed.id as id , ed.batch_num as batchNum , ed.identification_code as identificationCode ," +
                "ed.sensor_type as sensorType , ed.message_type as messageType , ed.results_frame as resultsFrame , " +
                "ed.update_time as updateTime , ed.create_time as createTime ";
        String sql = " from eq_equipment_data ed "+
                "LEFT JOIN eq_equipment eq ON eq.identification_code = ed.identification_code ";
        sql += "where 1=1 and ed.createTime >= DATE_SUB(NOW(),INTERVAL 1 MINUTE) ";
        Map<String, Object> param = new HashMap<>();
        //设备号
        if (!StringUtils.isEmpty(obj.getIdentificationCode().toString())){
            sql += " and ed.identification_code = :identificationCode";
            param.put("identificationCode" , obj.getIdentificationCode());
        }
        //传感器类型
        if (!StringUtils.isEmpty(obj.getSensorType())){
            sql += " and ed.sensor_type = :sensorType";
            param.put("sensorType" , obj.getSensorType());
        }
        //指令报文类型
        if (!StringUtils.isEmpty(obj.getMessageType().toString())){
            sql += " and ed.message_type = :messageType";
            param.put("messageType" , obj.getMessageType());
        }
        sql += " order by ed.id desc ";
        List<Map<String, Object>> contents = baseDao.findBySqlNoLimet(sqlLeft + sql, param);
        return contents;
    }

}
